package duty_info

import (
	"github.com/gin-gonic/gin"
	"log"
	"tao-admin/configs"
	"tao-admin/internal/entity"
	"tao-admin/internal/model"
	"tao-admin/utils/copier"
)

type DutyManager struct{}

type SearchDutyCondition struct {
	Project   *string `json:"project"`
	Key       *string `json:"key"`
	Type      *string `json:"type"`
	Value     *string `json:"value"`
	Duty      *string `json:"duty"`
	Component *string `json:"component"`
}

func (manager *DutyManager) SearchDutyByConditionMng(ctx *gin.Context, condition *SearchDutyCondition) ([]*entity.DutyInfoEntity, int64, error) {
	db := configs.GetGormDB()
	dutyList := make([]model.OnDutyTab, 0)
	var total int64 = 0

	if condition.Project != nil {
		db = db.Where("project=?", condition.Project)
	}
	if condition.Duty != nil {
		db = db.Where("duty=?", condition.Duty)
	}
	if condition.Key != nil {
		db = db.Where("key=?", condition.Key)
	}
	if condition.Value != nil {
		db = db.Where("value=?", condition.Value)
	}
	if condition.Type != nil {
		db = db.Where("type=?", condition.Type)
	}
	if condition.Component != nil {
		db = db.Where("component=?", condition.Component)
	}

	err1 := db.Model(&model.OnDutyTab{}).Count(&total).Error
	db = db.Order("project DESC, type ASC")
	err2 := db.Find(&dutyList).Error
	if err1 != nil || err2 != nil {
		return nil, 0, nil
	}

	log.Printf("查表结束, dutyList: %v", dutyList)
	//return dutyList, total, nil

	outDutyList := make([]*entity.DutyInfoEntity, 0)
	copyErr := copier.Copy(dutyList, &outDutyList)
	if copyErr != nil {
		return nil, 0, copyErr
	}
	log.Printf("查表结束, outDutyList: %v, total: %v", &outDutyList, total)
	return outDutyList, total, nil
}
